<template>
  <!-- 列表 -->

  <div class="resource-container">
    <div class="resource-aside" v-if="weeklist.length">
      <CollapseSelect
        disableTitleSelected
        v-model="curSelected"
        v-model:collapse="curCollapse"
        :list="weeklist"
        :titleFormat="titleFormat"
        :selectedFormat="selectedFormat"
        :childLableFormat="childLableFormat"
        childrenKey="weeksList"
        @selected-change="handleSelectedChange"
      />
    </div>
    <div class="resource-content">
      <el-container>
        <el-header>
          <div class="resource-header">
            <el-button
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['school:room-weeks:create']"
            >
              <Icon icon="ep:plus" class="mr-5px" /> 新增
            </el-button>
            <template v-if="curSelected">
              <!-- <el-button
                type="success"
                plain
                @click="handleExport"
                :loading="exportLoading"
                v-hasPermi="['school:classes:export']"
              >
                <Icon icon="ep:download" class="mr-5px" /> 导出
              </el-button> -->

              <el-button type="primary" @click="openForm('update', curSelected)"> 编辑 </el-button>
              <el-button type="danger" @click="handleDelete(curSelected)"> 删除 </el-button>
            </template>
            <!-- <view>
              <el-input
                v-model="queryParams.className"
                placeholder="请输入班级名称"
                clearable
                @keyup.enter="handleQuery"
                class="!w-160px"
              />
              <el-button type="primary" class="ml-10px" @click="handleQuery">
                <Icon icon="ep:search" class="mr-5px" /> 搜索
              </el-button>
            </view> -->
            <!-- <view> <span class="c-gray">班级人数:</span>28人 </view> -->
          </div>
        </el-header>
        <el-main v-loading="loading">
          <div class="file-content">
            <RoomWeeksDetail :list="weekData" />
            <!-- <ElEmpty v-else description="暂无周计划" /> -->
          </div>
        </el-main>
      </el-container>
    </div>
  </div>

  <!-- 表单弹窗：添加/修改 -->
  <RoomWeeksForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { RoomWeeksApi } from '@/api/school/room/weeks'
import RoomWeeksForm from './RoomWeeksForm.vue'
import RoomWeeksDetail from '../components/RoomWeeksDetail.vue'
const route = useRoute()

/** 功能室周时间 列表 */
defineOptions({ name: 'SchoolRoomWeeks' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const weeklist: any = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 100,
  roomType: '',
  weekYear: undefined,
  weekDateStart: undefined,
  weekDateEnd: undefined,
  weekNumberStart: undefined,
  weekNumberEnd: undefined,
  status: undefined,
  createTime: []
})
const weekData: any = ref([]) //
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const curSelected: any = ref('')
const curCollapse = ref(['0', '1'])
const selectedFormat = (item) => {
  return item.id
}
const handleSelectedChange = (id) => {
  console.log(id, '---id')
  getRoomScheduleListByWeekId(id)
}
/** 查询列表 */
const getList = async (notInit?: boolean) => {
  loading.value = true
  try {
    const data = await RoomWeeksApi.getWeekScheduleList(queryParams.roomType)
    data.forEach((item) => {
      item.weeksList.reverse()
    })
    weeklist.value = data
    if (!notInit) curSelected.value = weeklist.value?.[0]?.weeksList?.[0]?.id || ''
    await getRoomScheduleListByWeekId(curSelected.value)
  } finally {
    loading.value = false
  }
}
const getRoomScheduleListByWeekId = async (id) => {
  try {
    const data = await RoomWeeksApi.getRoomScheduleListByWeekId(id)
    weekData.value = data
  } finally {
  }
}
const titleFormat = (item: any) => {
  return `${item.weekYear}年`
}
const childLableFormat = (item: any) => {
  return item.weeksName || ''
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, queryParams.roomType, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await RoomWeeksApi.deleteRoomWeeks(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await RoomWeeksApi.exportRoomWeeks(queryParams)
    download.excel(data, '功能室周时间.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

// 路由到 categoryId 映射关系
const pathToTypeMap = {
  '/school/room/func/room-weeks': '1', //功能室
  '/school/room/game-zone/room-game': '2' //游戏区
  // 可以继续扩展更多路径
}

/** 监听路由，判断是从哪个菜单请求的，然后赋值不同的类型 */
watch(
  () => route.path,
  (path) => {
    console.log(path, '---path')

    // 匹配路径并赋值 resourceCode
    queryParams.roomType = pathToTypeMap[path] || '1'
    getList()

    console.log(queryParams.roomType, '---roomType')
  },
  {
    immediate: true //立即执行一次
  }
)
</script>
<style lang="scss" scoped>
.resource-container {
  display: flex;
  min-height: $content-height;

  .resource-aside {
    width: 200px;
    min-height: $content-height;
    background-color: #fff;
    border-right: solid 1px var(--el-menu-border-color);
    flex-shrink: 0;
  }

  .resource-content {
    height: $content-height;
    // background-color: #fff;
    flex: 1;
    overflow: hidden;
  }

  .file-content {
    display: flex;
    max-width: 1200px;
    margin: 0 auto;
    font-size: unset;
    line-height: unset;
    flex-wrap: wrap;
    gap: 20px 30px;
  }

  .resource-header {
    display: flex;
    height: 48px;
    // border-bottom: 1px solid #e9e9e9;
    align-items: center;
  }

  :deep() {
    .el-container {
      height: 100%;
    }

    .el-header {
      height: auto;
      background-color: #fff;
    }

    .el-empty {
      margin: 0 auto;
    }

    .el-collapse-item__header {
      padding-left: 16px;
    }

    .el-card {
      background-color: none;
    }
  }
}
</style>
